﻿using System;
using System.Linq;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using System.Collections.Generic;

namespace Wicresoft.EKA.ITSM.BusinessImpl
{
    public class SMGXJPlanTicketRuleRelated : Base, ISMGXJPlanTicketRuleRelated
    {
        protected override void Dispose(bool disposing)
        {
        }

        public string Create(SMGXJPlanTicketRuleRelatedModel model)
        {
            string result = "";
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanTicketRuleRelatedModel>();
                var list = proxy.GetListEntry("'cChr_TicketID' =\"" + model.cChr_TicketID + "\" and 'cChr_RuleID' = \"" + model.cChr_RuleID + "\" and 'cChr_ObjectID' = \"" + model.cChr_ObjectID + "\"", new System.Collections.Generic.List<uint>() { 
                    SMGXJPlanTicketRuleRelatedModel.RequestID_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_MarkAsDelete_ID
                }).ToList();
                if (model.cChr_MarkAsDelete == "No")
                {
                    if (list.Count() == 0)
                    {
                        proxy.CreateEntry(model);
                        result = "Success Create";
                        //result = model.cChr_RuleName;
                    }
                    else if (list.Count() != 0 && list.Exists(o => o.cChr_MarkAsDelete == "Yes"))
                    {
                        SMGXJPlanTicketRuleRelatedModel model2 = new SMGXJPlanTicketRuleRelatedModel();
                        model2.cChr_MarkAsDelete = "No";
                        proxy.SetEntryByQuery(model2, "'cChr_TicketID' =\"" + model.cChr_TicketID + "\" and 'cChr_RuleID' = \"" + model.cChr_RuleID + "\" and 'cChr_MarkAsDelete' = \"Yes\"", BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyFirst);
                        result = "Success but modify";
                        //result = model.cChr_RuleName;
                    }
                    else if (list.Count() != 0 && list.Exists(o => o.cChr_MarkAsDelete == "No"))
                    {
                        result = "Success but not action";
                        //result = model.cChr_RuleName;
                    }
                }
                else
                {
                    if (list.Count() != 0 && list.Exists(o => o.cChr_MarkAsDelete == "No"))
                    {
                        SMGXJPlanTicketRuleRelatedModel model3 = new SMGXJPlanTicketRuleRelatedModel();
                        model3.cChr_MarkAsDelete = "Yes";
                        proxy.SetEntryByQuery(model3, "'cChr_TicketID' =\"" + model.cChr_TicketID + "\" and 'cChr_RuleID' = \"" + model.cChr_RuleID + "\" and 'cChr_MarkAsDelete' = \"No\"", BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyFirst);
                        result = "Success but modify";
                    }
                }
            }
            catch (Exception e)
            {
                result = "Fail :" + e.Message;
            }
            return result;
        }

        public IEnumerable<SMGXJPlanTicketRuleRelatedModel> GetList(string Qual)
        {
            var proxy = this.GetARProxy<SMGXJPlanTicketRuleRelatedModel>();
            var list = proxy.GetListEntry(Qual, new System.Collections.Generic.List<uint>()
                {
                    SMGXJPlanTicketRuleRelatedModel.RequestID_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_MarkAsDelete_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_ObjectID_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_RuleID_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_RuleName_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_TicketID_ID,
                    SMGXJPlanTicketRuleRelatedModel.cChr_Char03_ID
                });
            return list;
        }

        public string Modify(SMGXJPlanTicketRuleRelatedModel model,string Qual)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanTicketRuleRelatedModel>();
                proxy.SetEntryByQuery(model,Qual, BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyAll);
                return "Success";
            }
            catch (Exception e)
            {
                return "Fail:" + e.Message;
            }
        }
    }
}
